/* test_alloc.c

 test for alloc(s):
1) util/gen_query.c > query.src
2) test_alloc.c < query.src  > query_loc.src   >query_flist.src
3) queryloc_to_bitmap.c < query_loc.src   > bitmap_loc
4) queryflist_to_bitmap.c < query_flist.src    > bitmap_flist  
5) cmp bitmap_loc bitmap_flist

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "buddy_system.h"
#include "defines.h"

int main()
{
	char* g_image;
	STATE state;
	int i,need,location;
	FILE *fd_from, *fd_to, *fd_flist;
	

/* init */
	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	sync_init("/tmp/disk", "sync.log");
	state=disk_init(INIT_TYPE_CREATE, g_image, NULL, "buddy.log", 50000);
/*********/

	disk_show();

	fd_from=fopen("query.src","r");
	fd_to=fopen("query_loc.src","w");
	fd_flist=fopen("query_flist.src","w");

	while (fscanf(fd_from,"%d",&need)!=EOF)
	{
		location=disk_alloc(need); 
		//printf ("[%d %d]\n",need, location);
		fprintf(fd_to, "%d %d\n", need, location);
	}
	
	disk_bitmap(fd_flist);

	fclose(fd_flist);
	fclose(fd_to);
	fclose(fd_from);

	disk_show();


/* exit */
	disk_exit();
	state=sync_write(DISK_IMAGE, g_image, IMAGE_K_SIZE*1024);
	if (state==-1) printf ("state error\n");
	sync_exit();		
	mp_free(g_image);

	return 0;
}
